package smap;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import BD.AlunoBD;
import BD.CursoBD;
import DAO.CursoDAO;
import BD.TeseBD;
import DAO.TeseDAO;
import BD.LinhaPesquisaBD;
import DAO.LinhaPesquisaDAO;

public class TelaCadTese extends javax.swing.JDialog {

    TeseDAO tesDao = new TeseDAO();
    TeseBD tes = new TeseBD();
    
    //Carregar combobox de curso
    CursoDAO curDao = new CursoDAO();
    List<CursoBD> listacurso = new ArrayList<CursoBD>();
    
    //Carregar combobox de linha de pesquisa
    LinhaPesquisaDAO lpDao = new LinhaPesquisaDAO();
    List<LinhaPesquisaBD> listalp = new ArrayList<LinhaPesquisaBD>();
    
    private int flagAnoIngresso;
    
    public TelaCadTese(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void carregarComboCurso(){
        listacurso = curDao.carregarComboCurso();
        
        for(int i = 0; i<listacurso.size(); i++){
            cmbCurso.addItem(listacurso.get(i));
        }
    }
    
    public void carregarComboLinhaPesq(){
        cmbLinhaPesq.removeAllItems(); //limpar combobox
        listalp.removeAll(listalp); //limpar a lista
        listalp = lpDao.carregarComboLinhaPesquisa();
        
        for(int i = 0; i<listalp.size(); i++){
            cmbLinhaPesq.addItem(listalp.get(i));
        }
    }
    
      
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlVoltar1 = new javax.swing.JPanel();
        btnVoltar1 = new javax.swing.JButton();
        pnlCadastrarTese = new javax.swing.JPanel();
        lblCodigo = new javax.swing.JLabel();
        lblRa = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtRa = new javax.swing.JTextField();
        lblAluno = new javax.swing.JLabel();
        txtAluno = new javax.swing.JTextField();
        lblTitulo = new javax.swing.JLabel();
        txtTitulo = new javax.swing.JTextField();
        lblLinhaPesq = new javax.swing.JLabel();
        cmbLinhaPesq = new javax.swing.JComboBox();
        btnTelaTese = new javax.swing.JButton();
        btnCadastrar = new javax.swing.JButton();
        pnlSelecioneAluno = new javax.swing.JPanel();
        lblCurso = new javax.swing.JLabel();
        cmbCurso = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAluno = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jycAnoIngresso = new com.toedter.calendar.JYearChooser();
        pnlTesesCadastradas = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblTese = new javax.swing.JTable();
        pnlPesquisar = new javax.swing.JPanel();
        cmbPesquisar = new javax.swing.JComboBox();
        txtPesquisar = new javax.swing.JTextField();
        btnPesquisar = new javax.swing.JButton();
        btnCarregarTodos = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnMarcarAvaliacao = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Tese");
        setResizable(false);

        pnlVoltar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar1.setText("Voltar");
        btnVoltar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlVoltar1Layout = new javax.swing.GroupLayout(pnlVoltar1);
        pnlVoltar1.setLayout(pnlVoltar1Layout);
        pnlVoltar1Layout.setHorizontalGroup(
            pnlVoltar1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlVoltar1Layout.createSequentialGroup()
                .addGap(476, 476, 476)
                .addComponent(btnVoltar1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlVoltar1Layout.setVerticalGroup(
            pnlVoltar1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlVoltar1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlCadastrarTese.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Cadastrar tese"));

        lblCodigo.setText("* Código:");

        lblRa.setText("* RA:");

        txtCodigo.setEnabled(false);

        txtRa.setEnabled(false);

        lblAluno.setText("Aluno:");

        txtAluno.setEnabled(false);

        lblTitulo.setText("* Título:");

        lblLinhaPesq.setText("* Linha de Pesquisa:");

        btnTelaTese.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/area-icon.png"))); // NOI18N
        btnTelaTese.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTelaTeseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlCadastrarTeseLayout = new javax.swing.GroupLayout(pnlCadastrarTese);
        pnlCadastrarTese.setLayout(pnlCadastrarTeseLayout);
        pnlCadastrarTeseLayout.setHorizontalGroup(
            pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                        .addComponent(lblCodigo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblRa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtRa))
                    .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                        .addComponent(lblLinhaPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbLinhaPesq, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnTelaTese))
                    .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                        .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTitulo)
                            .addComponent(lblAluno))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAluno)
                            .addComponent(txtTitulo))))
                .addContainerGap())
        );
        pnlCadastrarTeseLayout.setVerticalGroup(
            pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCadastrarTeseLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodigo)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRa)
                    .addComponent(txtRa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAluno)
                    .addComponent(txtAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTitulo)
                    .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlCadastrarTeseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblLinhaPesq)
                        .addComponent(cmbLinhaPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnTelaTese))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnCadastrar.setText("Cadastrar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        pnlSelecioneAluno.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Selecione o aluno"));

        lblCurso.setText("Curso:");

        cmbCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbCursoActionPerformed(evt);
            }
        });

        tblAluno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "RA", "Nome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAluno.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblAluno.getTableHeader().setReorderingAllowed(false);
        tblAluno.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAlunoMouseClicked(evt);
            }
        });
        tblAluno.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblAlunoKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblAluno);
        tblAluno.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblAluno.getColumnModel().getColumn(0).setMinWidth(70);
        tblAluno.getColumnModel().getColumn(0).setPreferredWidth(70);
        tblAluno.getColumnModel().getColumn(0).setMaxWidth(80);

        jLabel1.setText("Ingresso:");

        jycAnoIngresso.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jycAnoIngressoPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout pnlSelecioneAlunoLayout = new javax.swing.GroupLayout(pnlSelecioneAluno);
        pnlSelecioneAluno.setLayout(pnlSelecioneAlunoLayout);
        pnlSelecioneAlunoLayout.setHorizontalGroup(
            pnlSelecioneAlunoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSelecioneAlunoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jycAnoIngresso, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCurso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbCurso, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
        );
        pnlSelecioneAlunoLayout.setVerticalGroup(
            pnlSelecioneAlunoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSelecioneAlunoLayout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(pnlSelecioneAlunoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlSelecioneAlunoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblCurso)
                        .addComponent(cmbCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addComponent(jycAnoIngresso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pnlTesesCadastradas.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Teses cadastradas"));

        tblTese.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Ra", "Aluno", "Título", "Linha de Pesquisa", "cod_linhapesquisa", "Qualificação", "Defesa"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblTese.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblTese.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblTese);
        tblTese.getColumnModel().getColumn(0).setMinWidth(0);
        tblTese.getColumnModel().getColumn(0).setPreferredWidth(0);
        tblTese.getColumnModel().getColumn(0).setMaxWidth(0);
        tblTese.getColumnModel().getColumn(1).setMinWidth(0);
        tblTese.getColumnModel().getColumn(1).setPreferredWidth(0);
        tblTese.getColumnModel().getColumn(1).setMaxWidth(0);
        tblTese.getColumnModel().getColumn(5).setMinWidth(0);
        tblTese.getColumnModel().getColumn(5).setPreferredWidth(0);
        tblTese.getColumnModel().getColumn(5).setMaxWidth(0);
        tblTese.getColumnModel().getColumn(6).setMinWidth(100);
        tblTese.getColumnModel().getColumn(6).setPreferredWidth(100);
        tblTese.getColumnModel().getColumn(6).setMaxWidth(110);
        tblTese.getColumnModel().getColumn(7).setMinWidth(70);
        tblTese.getColumnModel().getColumn(7).setPreferredWidth(70);
        tblTese.getColumnModel().getColumn(7).setMaxWidth(90);

        pnlPesquisar.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Pesquisar"));

        cmbPesquisar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Aluno", "Título", "Qualificada", "Defendida" }));
        cmbPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbPesquisarActionPerformed(evt);
            }
        });

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/procurar-icon.png"))); // NOI18N
        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnCarregarTodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/todos-icon.png"))); // NOI18N
        btnCarregarTodos.setText("Carregar todos");
        btnCarregarTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregarTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPesquisarLayout = new javax.swing.GroupLayout(pnlPesquisar);
        pnlPesquisar.setLayout(pnlPesquisarLayout);
        pnlPesquisarLayout.setHorizontalGroup(
            pnlPesquisarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPesquisarLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPesquisarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPesquisarLayout.createSequentialGroup()
                        .addComponent(cmbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPesquisar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPesquisarLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCarregarTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnlPesquisarLayout.setVerticalGroup(
            pnlPesquisarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPesquisarLayout.createSequentialGroup()
                .addGroup(pnlPesquisarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPesquisarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPesquisar)
                    .addComponent(btnCarregarTodos))
                .addGap(0, 12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnlTesesCadastradasLayout = new javax.swing.GroupLayout(pnlTesesCadastradas);
        pnlTesesCadastradas.setLayout(pnlTesesCadastradasLayout);
        pnlTesesCadastradasLayout.setHorizontalGroup(
            pnlTesesCadastradasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
            .addComponent(pnlPesquisar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnlTesesCadastradasLayout.setVerticalGroup(
            pnlTesesCadastradasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlTesesCadastradasLayout.createSequentialGroup()
                .addComponent(pnlPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
        );

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alterar-icon.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/delete-icon.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnMarcarAvaliacao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/banca-icon.png"))); // NOI18N
        btnMarcarAvaliacao.setText("Marcar avaliação");
        btnMarcarAvaliacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMarcarAvaliacaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlVoltar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnlSelecioneAluno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlCadastrarTese, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlTesesCadastradas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(232, 232, 232)
                .addComponent(btnMarcarAvaliacao, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlSelecioneAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlCadastrarTese, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlTesesCadastradas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAlterar)
                        .addComponent(btnExcluir)
                        .addComponent(btnMarcarAvaliacao))
                    .addComponent(btnCadastrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlVoltar1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnVoltar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltar1ActionPerformed
        this.dispose(); // Fecha Tela de Cadastro de Tese
    }//GEN-LAST:event_btnVoltar1ActionPerformed

    private void btnTelaTeseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTelaTeseActionPerformed
        this.setEnabled(false);
        TelaLinhaPesquisa lp = new TelaLinhaPesquisa(null, true);
        lp.carregarTabela(); //carregar tabela da tela linha de pesquisa
        lp.setVisible(true); //abrir tela linha de pesquisa
        carregarComboLinhaPesq(); //recarregar combo de linha de pesquisa
        carregarTabela(""); //recarregar a tabela de teses
        this.setEnabled(true);
    }//GEN-LAST:event_btnTelaTeseActionPerformed

    private void cmbCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbCursoActionPerformed
        if(cmbCurso.getSelectedIndex() != -1){ // tratamento de erro se combo estiver vazio
            carregarAluno();
        }
        else{
            DefaultTableModel tbm = (DefaultTableModel)tblAluno.getModel();         

            //Remove as linhas da Jtable, para limpar.
            for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
                tbm.removeRow(i); 
            } 
            int i = 0; 
        }
        //carregar os dados da tese (só pra limpar)
        carregarDadosTese();
    }//GEN-LAST:event_cmbCursoActionPerformed

    private void tblAlunoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAlunoMouseClicked
        carregarDadosTese();
    }//GEN-LAST:event_tblAlunoMouseClicked

    private void tblAlunoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblAlunoKeyReleased
        carregarDadosTese();
    }//GEN-LAST:event_tblAlunoKeyReleased

    private void btnCarregarTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregarTodosActionPerformed
        carregarTabela("");
    }//GEN-LAST:event_btnCarregarTodosActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        carregarTabela(txtPesquisar.getText().trim());
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        if(txtCodigo.getText().trim().length() == 0 || txtRa.getText().trim().length() == 0 
                || txtTitulo.getText().trim().length() == 0 
                || cmbLinhaPesq.getSelectedIndex() == -1 || cmbLinhaPesq.getSelectedIndex() == 0){
             
            JOptionPane.showMessageDialog(null,"Campos código, ra, título e"
                    + " linha de pesquisa são obrigatórios", 
                    "Campos obrigatórios", JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            //seta os dados
            tes.setCodigo(Integer.parseInt(txtCodigo.getText().trim()));
            tes.setTitulo(txtTitulo.getText().trim());
            tes.setRa_al(Integer.parseInt(txtRa.getText().trim()));
            tes.setCodLinhaPesquisa(listalp.get(cmbLinhaPesq.getSelectedIndex()).getCodigo());
            tes.setNomeAluno(txtAluno.getText().trim());
            tes.setCod_qualificacao(false);
            tes.setCod_defesa(false);

            if(tesDao.inserirTese(tes)){
                autoIncremento();
                txtTitulo.setText("");
                carregarDadosTese();
                carregarTabela("");
            }
        }
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
       Integer linha = tblTese.getSelectedRow();
       if(linha == -1){
            JOptionPane.showMessageDialog(null,"Selecione uma tese para excluir",
                    "Selecione a tese", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            Object[] opcoes = { "Sim", "Não" };
                //Obtem os dados de acordo com a linha ao qual foi selecionada.

            Integer resposta =JOptionPane.showOptionDialog(null,"Deseja excluir esta tese ?", 
                    "Exclusão",JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null,
                    opcoes, opcoes[1]);

            // Verifica Resposta do Usuário
            if(resposta == JOptionPane.OK_OPTION){
                //seta o código da disciplina para exclusão
                tes.setCodigo((Integer)tblTese.getValueAt(linha, 0));

                //excluir passando o codigo
                if(tesDao.excluirTese(tes)){
                    JOptionPane.showMessageDialog(null,"Exclusão realizada com sucesso", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);
                    //recarregar o jTable
                    carregarTabela("");
                }
            }
            else{
                JOptionPane.showMessageDialog(null,"Exclusão cancelada", "Exclusão", JOptionPane.INFORMATION_MESSAGE);
            }           
       }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        Integer linha = tblTese.getSelectedRow();
        if(linha == -1){
            JOptionPane.showMessageDialog(null,"Selecione uma tese para alterar",
                    "Selecione a tese", JOptionPane.INFORMATION_MESSAGE);    
        }else{
            //seta os dados para enviar pra tela alterar
            tes.setCodigo((Integer)tblTese.getValueAt(linha, 0));
            tes.setRa_al((Integer)tblTese.getValueAt(linha, 1));
            tes.setNomeAluno((String)tblTese.getValueAt(linha, 2));
            tes.setTitulo((String)tblTese.getValueAt(linha, 3));
            tes.setLinhaPesquisa((String)tblTese.getValueAt(linha, 4));
            tes.setCodLinhaPesquisa((Integer)tblTese.getValueAt(linha, 5));
            
            this.setEnabled(false);
            TelaAltTese altTese = new TelaAltTese(null, true);
            altTese.carregarComboLinhaPesq(); //carregar combo de linha de pesquisa
            altTese.recebeObjejo(tes);
            altTese.setVisible(true);
            this.setEnabled(true);
            //recarregar tabela
            carregarTabela("");
        }
        carregarComboLinhaPesq();
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void jycAnoIngressoPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jycAnoIngressoPropertyChange
        if(flagAnoIngresso == 0){
            flagAnoIngresso = 1;
        }else
        {
            if(cmbCurso.getSelectedIndex() != -1){
                carregarAluno();
            }
        }
    }//GEN-LAST:event_jycAnoIngressoPropertyChange

    private void btnMarcarAvaliacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMarcarAvaliacaoActionPerformed
        int linha = tblTese.getSelectedRow();
        if(linha != -1){   
            Object[] opcoes = { "Defesa", "Qualificação" };

            Integer resposta =JOptionPane.showOptionDialog(null,"Escolha o tipo de avaliação de tese.", 
                    "Avaliação de tese",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    opcoes, opcoes[1]);

            // Verifica Resposta do Usuário
            if(resposta == JOptionPane.OK_OPTION){//defesa
                TelaDefesa teldef = new TelaDefesa(null, true);
                teldef.setCodTeseRecebido((Integer)(tblTese.getValueAt(linha, 0)));
                teldef.carregarTabela(String.valueOf(tblTese.getValueAt(linha, 0)));
                teldef.setVisible(true);
                carregarTabela("");
            }else{//qualificação
                TelaQualificacao telqua = new TelaQualificacao(null, true);
                telqua.setCodTeseRecebido((Integer)(tblTese.getValueAt(linha, 0)));
                telqua.carregarTabela(String.valueOf(tblTese.getValueAt(linha, 0)));
                telqua.setVisible(true);
                carregarTabela("");
            }    
        }else{
            JOptionPane.showMessageDialog(null, "Selecione uma tese para marcar o exame de qualificação.", 
                    "Selecione a tese", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnMarcarAvaliacaoActionPerformed

    private void cmbPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPesquisarActionPerformed
        if(cmbPesquisar.getSelectedIndex() == 2 || cmbPesquisar.getSelectedIndex() == 3){
            txtPesquisar.setEnabled(false);
            txtPesquisar.setText("");
        }
        else
        {
            txtPesquisar.setEnabled(true);
        }
    }//GEN-LAST:event_cmbPesquisarActionPerformed
    
    private void carregarDadosTese(){
        Integer linha = tblAluno.getSelectedRow();
        
        if(linha != -1){
            //seta os campos de acordo com a linha selecionada
            txtRa.setText(String.valueOf(tblAluno.getValueAt(linha, 0)));
            txtAluno.setText(String.valueOf(tblAluno.getValueAt(linha, 1)));
        }else{
            //limpa os campos
            txtAluno.setText("");
            txtRa.setText("");
        }
    }
    
    public void autoIncremento(){
        txtCodigo.setText(String.valueOf(tesDao.autoIncremento()));          
    }
    
    public void carregarTabela(String valor){
        //cria lista de objetos do tipo tese
        List<TeseBD> listaTese = new ArrayList<TeseBD>();
        
        if(cmbPesquisar.getSelectedIndex() == 0){          
            listaTese = tesDao.filtro("a.nome_al", valor);
        }else{
             if(cmbPesquisar.getSelectedIndex() == 1){          
                listaTese = tesDao.filtro("t.titulo_tese", valor);
             }
             else
             {
                 if(cmbPesquisar.getSelectedIndex() == 2){          
                     listaTese = tesDao.filtro("t.qualificada_tese", "1");
                 }else{
                     if(cmbPesquisar.getSelectedIndex() == 3){
                          listaTese = tesDao.filtro("t.defendida_tese", "1");
                     }
                 }
             }
        }

        DefaultTableModel tbm = (DefaultTableModel)tblTese.getModel();         
        
        //limpa a jtable
        for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
            tbm.removeRow(i); 
        } 
        int i = 0; 
        
        //preenche a jTable
        for(TeseBD us : listaTese){            
            tbm.addRow(new String[1]); 
            tblTese.setValueAt(us.getCodigo(), i, 0); //Objeto Linha Coluna
            tblTese.setValueAt(us.getRa_al(), i, 1);
            tblTese.setValueAt(us.getNomeAluno(), i, 2); 
            tblTese.setValueAt(us.getTitulo(), i, 3);
            tblTese.setValueAt(us.getLinhaPesquisa(), i, 4);            
            //atributo escondido na jtable
            tblTese.setValueAt(us.getCodLinhaPesquisa(), i, 5);
            //
            tblTese.setValueAt(us.getCod_qualificacao(), i, 6);
            tblTese.setValueAt(us.getCod_defesa(), i, 7);
            i++; 
        }
        
        //centraliza as colunas desejadas
        centralizarTable();
    }
           
    public void carregarAluno(){
        List<AlunoBD> listaal = new ArrayList<AlunoBD>(); 

        //Carrega os alunos cadastrados no curso
        listaal = tesDao.filtroAluno(cmbCurso.getSelectedItem().toString(), String.valueOf(jycAnoIngresso.getValue()));
        
        //define um modelo para a Jtable
        DefaultTableModel tbm = (DefaultTableModel)tblAluno.getModel();         
        
        //Remove as linhas da Jtable, para limpar.
        for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
            tbm.removeRow(i); 
        } 
        int i = 0; 
        
        //Para cada aluno que ele encontrar, adiciona uma nova linha na Jtable
        for(AlunoBD al : listaal){            
            tbm.addRow(new String[1]); 
            tblAluno.setValueAt(al.getRa(), i, 0);
            tblAluno.setValueAt(al.getNome(), i, 1);          
            i++; 
        }
        centralizarTable();
    }
    
    private void centralizarTable(){
        //Centralizar os dados na tabela.
        DefaultTableCellRenderer center = new DefaultTableCellRenderer();
        center.setHorizontalAlignment(SwingConstants.CENTER ); 
        tblAluno.getColumnModel().getColumn(0).setCellRenderer(center);
        tblTese.getColumnModel().getColumn(0).setCellRenderer(center);
    }
    
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadTese.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaCadTese dialog = new TelaCadTese(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCarregarTodos;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnMarcarAvaliacao;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnTelaTese;
    private javax.swing.JButton btnVoltar1;
    private javax.swing.JComboBox cmbCurso;
    private javax.swing.JComboBox cmbLinhaPesq;
    private javax.swing.JComboBox cmbPesquisar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private com.toedter.calendar.JYearChooser jycAnoIngresso;
    private javax.swing.JLabel lblAluno;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblCurso;
    private javax.swing.JLabel lblLinhaPesq;
    private javax.swing.JLabel lblRa;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPanel pnlCadastrarTese;
    private javax.swing.JPanel pnlPesquisar;
    private javax.swing.JPanel pnlSelecioneAluno;
    private javax.swing.JPanel pnlTesesCadastradas;
    private javax.swing.JPanel pnlVoltar1;
    private javax.swing.JTable tblAluno;
    private javax.swing.JTable tblTese;
    private javax.swing.JTextField txtAluno;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtPesquisar;
    private javax.swing.JTextField txtRa;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables
}
